#/usr/bin/env python3
# -*- coding: utf-8 -*-
# 自己写的类实现with方法

from contextlib import contextmanager

# 原始方法
"""
class Query(object):

    def __init__(self, name):
        self.name = name

    def __enter__(self):
        print('Begin')
        return self
    
    def __exit__(self, exc_type, exc_value, traceback):
        if exc_type:
            print('Error')
        else:
            print('End')
    
    def query(self):
        print('Query info about %s...' % self.name)

with Query('lidi') as q:
    q.query()

# 使用context
class Query(object):
    
    def __init__(self, name):
        self.name = name
    
    def query(self):
        print('Query info about %s ...' % self.name)

@contextmanager
def create_query(name):
    print('Begin')
    q = Query(name)
    yield q
    print('End')

with create_query as q:
    q.query()

"""

@contextmanager
def tag(name):
    print("<%s>" % name)
    yield
    print("</%s>" % name)

with tag("h1"):
    print("hello")
    print("world")















